package com.zhongwang.mms.util;

import com.zhongwang.mms.gen.entity.TCost;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目号转换工具类
 *
 * @Author: China.sgl
 * @Date: 2020/6/5 08:23
 */
public class CodeUtil {
    public static List<TCost> TCosts = new ArrayList<TCost>();
    public static String projectCodeHandle(String projectCode) {
        TCost tCost = new TCost();
        tCost.setCosId(1);
        tCost.setCosProCode("150000");
        tCost.setCosProName("机械工程部采购项目号");
        TCosts.add(tCost);
        tCost = new TCost();
        tCost.setCosId(2);
        tCost.setCosProCode("15000A");
        tCost.setCosProName("机械设计院采购项目号");
        tCost = new TCost();
        tCost.setCosId(3);
        tCost.setCosProCode("15000W");
        tCost.setCosProName("人力资源综合部采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(4);
        tCost.setCosProCode("18000A");
        tCost.setCosProName("生产运营部采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(5);
        tCost.setCosProCode("18000B");
        tCost.setCosProName("质检部采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(6);
        tCost.setCosProCode("15000L");
        tCost.setCosProName("供应部采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(7);
        tCost.setCosProCode("18000C");
        tCost.setCosProName("铆焊一分厂采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(8);
        tCost.setCosProCode("16000J");
        tCost.setCosProName("铆焊二分厂采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(9);
        tCost.setCosProCode("18000D");
        tCost.setCosProName("机加分厂采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(10);
        tCost.setCosProCode("19000A");
        tCost.setCosProName("电装分厂采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(11);
        tCost.setCosProCode("15000M");
        tCost.setCosProName("设备科采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(12);
        tCost.setCosProCode("140000");
        tCost.setCosProName("外厂串料项目号");
        TCosts.add(tCost);
        tCost.setCosId(13);
        tCost.setCosProCode("18000E");
        tCost.setCosProName("项目管理科采购项目号");
        TCosts.add(tCost);
        tCost.setCosId(14);
        tCost.setCosProCode("JJ0024");
        tCost.setCosProName("机加车间专用");
        TCosts.add(tCost);
        tCost.setCosId(15);
        tCost.setCosProCode("Y00024");
        tCost.setCosProName("运输车辆专用");
        TCosts.add(tCost);
        tCost.setCosId(16);
        tCost.setCosProCode("S00024");
        tCost.setCosProName("设备维修类");
        TCosts.add(tCost);
        tCost.setCosId(17);
        tCost.setCosProCode("D00024");
        tCost.setCosProName("电气件通用类");
        TCosts.add(tCost);
        tCost.setCosId(18);
        tCost.setCosProCode("L00024");
        tCost.setCosProName("劳保用品");
        TCosts.add(tCost);
        tCost.setCosId(19);
        tCost.setCosProCode("C00024");
        tCost.setCosProName("常用油漆");
        TCosts.add(tCost);
        tCost.setCosId(20);
        tCost.setCosProCode("B00024");
        tCost.setCosProName("办公用品");
        TCosts.add(tCost);
        tCost.setCosId(21);
        tCost.setCosProCode("G00024");
        tCost.setCosProName("工具、设备");
        TCosts.add(tCost);
        tCost.setCosId(22);
        tCost.setCosProCode("JX0024");
        tCost.setCosProName("机械内需");
        TCosts.add(tCost);
        tCost.setCosId(23);
        tCost.setCosProCode("GC0024");
        tCost.setCosProName("项目预留（钢材）");
        TCosts.add(tCost);
        tCost.setCosId(24);
        tCost.setCosProCode("LC0024");
        tCost.setCosProName("项目预留（铝材））");
        TCosts.add(tCost);
        tCost.setCosId(25);
        tCost.setCosProCode("WG0024");
        tCost.setCosProName("项目预留（外购）");
        TCosts.add(tCost);
        tCost.setCosId(26);
        tCost.setCosProCode("Z00024");
        tCost.setCosProName("质量科消耗/备件品");
        TCosts.add(tCost);
        tCost.setCosId(27);
        tCost.setCosProCode("X00024");
        tCost.setCosProName("消耗品");
        TCosts.add(tCost);
        String handleCode = "";
        try {
            if (!projectCode.trim().equals("") && projectCode.trim().toCharArray().length > 1) {
                String beginStr = projectCode.trim().toCharArray()[0]+""+projectCode.trim().toCharArray()[1];
                char lastChar = projectCode.trim().toCharArray()[projectCode.trim().length()-1];
                boolean isCost = false;
                for (int i = 0;i<TCosts.size();i++)
                {
                    if (TCosts.get(i).getCosProCode().equals(projectCode))
                    {
                        isCost = true;
                    }
                }
                //如果是不是成本中心的项目号前面不补20
                if(!isCost)
                {
                    //如果是ZW字母开头的 或 整个项目号是纯数字的 或 最后一个是字母的 前面不补20
                    if (beginStr.toUpperCase().equals("ZW")  || isNumeric(projectCode.trim()) || lastChar >='a' && lastChar <= 'z' || lastChar >='A' && lastChar <= 'Z') {
                        handleCode = projectCode.trim();
                    }
                    //其他补20
                    else {
                        if (projectCode.trim().length() == 8) {
                            handleCode = projectCode.trim().substring(2);
                        } else {
                            handleCode = projectCode.trim();
                        }
                        String year = new SimpleDateFormat("YYYY").format(new Date());
                        if (Integer.parseInt(year.substring(0, 2)) > 20) {
                            handleCode = year.substring(0, 2) + handleCode;
                        } else {
                            handleCode = "20" + handleCode;
                        }
                    }
                }else{
                    handleCode = projectCode.trim();
                }
            }else
            {
                handleCode = projectCode.trim();
            }
        }catch (Exception e){
            e.printStackTrace();
            return projectCode.toUpperCase();
        }
        return handleCode.toUpperCase();
    }


    /**
     * 判断字符串是否全为数字
     *
     * @param str 字符串
     * @return boolean
     */
    private static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57) {
                return false;
            }
        }
        return true;
    }
}
